import vueResourse from 'vue-resourse'
// import Cookies from 'cookie'

const AJAX_URL_PREFIX = JSON.stringify('bk_api')

const BASE_URL = window.PROJECT_CONFIG.SITE_URL + AJAX_URL_PREFIX

// vueResource 实例
const instance = vueResourse.create({
  withCredentials: true,
  headers: { 'X-REQUESTED-WITH': 'XMLHttpRequest',
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  },
  baseURL: BASE_URL
})
// http request 请求拦截器
instance.interceptors.request.use(
  config => {
    let token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// http response 拦截器
instance.interceptors.response.use(
  response => {
    response.headers['Content-Type'] = 'application/json'
  },
  error => {
    return Promise.reject(error) // 返回接口返回的错误信息
  }
)

export default function () {
  return vueResourse
}
